﻿// //  ********************************************************************************
// //  Copyright (C) 2016 Sebastian Kenter. All rights reserved.
// // 
// //  Name    : MultiServerEngine | ServerFramework | DefaultServerEventHandler.cs
// //  Created : 2016 - 02 - 16 20:53
// //  Modified: 2016 - 02 - 20 12:33
// //  ********************************************************************************

using ExitGames.Logging;
using Photon.SocketServer;
using ServerFramework.Implementation.Server;
using ServerFramework.Interfaces.Server;
using ServerFramework.Interfaces.Telegram;
using TypeDefs;

namespace ServerFramework.Implementation.Handler.Default
{
    public class DefaultServerEventHandler : ServerTelegramHandler
    {
        public override TelegramHandlerTypes HandlerType => TelegramHandlerTypes.Event;
        public override byte Code => (byte) OperationCodes.DefaultEvent;
        public override int? SubCode => null;

        public DefaultServerEventHandler(ILogger logger) : base(logger)
        {
        }

        public override bool HandleTelegram(ITelegram telegram, IServerPeer serverPeer)
        {
            Logger.InfoFormat("No handler found for telegram. Using default to prevent misbehavior.");
            ServerPeer peer = serverPeer as ServerPeer;

            // Send something back to the requesting server so that error handling can take place.
            peer?.SendOperationResponse(
                new OperationResponse(telegram.Code)
                {
                    ReturnCode = (short) ErrorCodes.InvalidOperation,
                    DebugMessage = "Telegram could not be handled"
                },
                new SendParameters());

            return true;
        }
    }
}
